package datastructure.hashtable;


/**
 * 散列表
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-27
 */
public class HashTableDemo {

    public static void main(String[] args) {
        Emp emp = new Emp(1, "tom");
        HashTable hashTable = new HashTable(7);
        hashTable.add(emp);
        hashTable.list();
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
        Emp emp1 = hashTable.findById(1);
        System.out.println(emp1);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
        hashTable.deleteById(1);
        hashTable.list();
    }
}

class HashTable {
    private EmpLinkedList[] empLinkedList;
    private int size;

    public HashTable(int size) {
        this.size = size;
        empLinkedList = new EmpLinkedList[size];
        // 注意 需要为每条链表初始化
        for (int i = 0; i < size; i++) {
            empLinkedList[i] = new EmpLinkedList();
        }
    }

    /**
     * 添加
     *
     * @param emp
     */
    public void add(Emp emp) {
        int index = hashFun(emp.id);
        empLinkedList[index].add(emp);
    }

    /**
     * 遍历hashtable
     */
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedList[i].list();
        }
    }

    /**
     * 通过id查找
     *
     * @param id
     * @return
     */
    public Emp findById(int id) {
        // 确定哪条链表
        int index = hashFun(id);
        Emp emp = empLinkedList[index].findById(id);
        if (emp == null) {
            System.out.println("没有找到");
        } else {
            System.out.println("在" + (index + 1) + "条链表中找到：" + emp);
        }
        return emp;
    }

    /**
     * 通过id删除
     *
     * @param id
     */
    public void deleteById(int id) {
        int index = hashFun(id);
        empLinkedList[index].deleteById(id);
    }


    /**
     * 散列函数
     *
     * @param id
     * @return
     */
    public int hashFun(int id) {
        return id % size;
    }


}

/**
 * 链表
 */
class EmpLinkedList {
    private Emp head;

    /**
     * 添加
     *
     * @param emp 雇员
     */
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (curEmp.next != null) {
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
    }

    /**
     * 通过id查找
     *
     * @param id
     * @return
     */
    public Emp findById(int id) {
        Emp emp = null;
        if (head == null) {
            return emp;
        }
        Emp curEmp = head;
        while (curEmp != null) {
            if (curEmp.id == id) {
                emp = curEmp;
                break;
            }
            curEmp = curEmp.next;
        }
        return emp;
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(int id) {
        if (head == null) {
            System.out.println("链表为空~~~~");
            return;
        }
        Emp curEmp = head;
        while (curEmp != null) {
            if (curEmp.next != null && curEmp.next.id == id) {
                curEmp.next = curEmp.next.next;
                break;
            }
            curEmp = curEmp.next;
        }
    }

    /**
     * 遍历连表
     */
    public void list() {
        if (head == null) {
            System.out.println("链表为空~~~~");
            return;
        }
        Emp curEmp = head;
        while (curEmp != null) {
            System.out.println(curEmp);
            curEmp = curEmp.next;
        }
    }

}

class Emp {

    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}